<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2016/12/27
 * Time: 21:52
 */
//逻辑代码
$dir = dirname(__FILE__);//根目录
//var_dump($dir);exit;
require $dir.'/db.php';//引入操作数据库
//require $dir.'/dbconfig.php';
require $dir.'/Phpexcel/PHPExcel.class.php';//引入phpexcel文件
$db = new db($phpexcel);//实例化DB类，连接数据库
//var_dump($db);exit;
$objPHPExcel = new PHPExcel();//实例化phpexcel类
for($i=1;$i<=3;$i++){
    if ($i>1){
        $objPHPExcel->createSheet();//创建新的内置表
    }
    $objPHPExcel->setActiveSheetIndex($i-1);//吧新建的sheet设定为当前活动sheet
    $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动sheet的操作对象
    $data = $db->getDateByGrade($i);//查询每个年级的学生的数据
    //var_dump($data);exit;
    $objSheet->setCellValue('A1','姓名')->setCellValue('B1','分数')->setCellValue('C1','班级');
    $j=2;
    foreach ($data as $key =>$val){
        $objSheet->setCellValue('A'.$j,$val['username'])
                 ->setCellValue('B'.$j,$val['score'])
                 ->setCellValue('C'.$j,$val['class'].'班');
        $j++;

    }
    }
$objWrite = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWrite->save($dir.'/calss.xlsx');//保存在生成的PHPexcel文件



